3. Upload program

The microcontroller follows rules to control OBLOQ and device. These rules are coded in the controller program which is then uploaded to the controller board. After building the hardware, the next step is uploading required program to Arduinio UNO.

Attention: Please verify below wifi configuration and iot account information according to your own needs.

//meanings: wifi ssid(DFRobot-guest), wifi password(dfrobot@2017), iot_id(Skv3zKyNb), iot_password(r1lD3ztJ4b)
Obloq olq(&softSerial,"DFRobot-guest","dfrobot@2017","Skv3zKyNb","r1lD3ztJ4b"); 

//Iot deviceTopic:BJpHJt1VW
const String devTopic = "BJpHJt1VW";

Here is sample code: (As the version may be updated, if sample code cannot be used, please check latest code repository)

#include <SoftwareSerial.h>
#include "Obloq.h"

SoftwareSerial softSerial(10,11);
//生成OBLOQ对象,参数:串口指针,wifiSsid, WifiPwd, iotId, iotPwd
//Generate OBLOQ object, parameters: serial pointer, wifiSsid, WifiPwd, iotId, iotPwd
Obloq olq(&softSerial,"DFRobot-guest","dfrobot@2017","Skv3zKyNb","r1lD3ztJ4b");
const String devTopic = "BJpHJt1VW";
 //led小灯引脚 led pin
int ledPin = 13;

//已监听设备的消息回调函数,可以在这个函数里面对接收的消息做判断和相应处理,需要用setMsgHandle()来设置这个回调函数
//message callback function of the subscribed device. This function handle the message received. setMsgHandle() will change settings of this function.
void msgHandle(const String& topic,const String& message)
{
    if(devTopic == topic)
    {
        if(message == "1")
          digitalWrite(ledPin,LOW);
        else if(message == "2")
          digitalWrite(ledPin,HIGH);
    }
}

void setup()
{
    pinMode(ledPin,OUTPUT);
    softSerial.begin(9600);
    olq.setMsgHandle(msgHandle);//注册消息回调函数 register message callback function
    olq.subscribe(devTopic); //监听设备 Subscribe topic
}
void loop()
{
    olq.update();
}

After program is uploaded, press "Reset" to restart OBLOQ. Then press "Reset" to restart Arduino UNO. Then wait for OBLOQ module connect to DF-IoT automatically.

(addendum 1) Sample code: publish message

The above sample code mainly talks about such a process: The DF-IoT website sends data to Obloq. When Obloq listens to the data, it passes the data to the Arduino to control light on and off. In return, can Obloq send data to the DF-IoT website? Of course it can. Here is a sample code of Obloq publishing message to DF-IoT every 10 seconds.

Sample code: (As the version may be updated, if sample code cannot be used, please check latest code repository)

#include "Arduino.h"
#include "SoftwareSerial.h"
#include "Obloq.h"

SoftwareSerial softSerial(10,11);
//生成OBLOQ对象,参数:串口指针,wifiSsid,WifiPwd,iotId,iotPwd
//Generate OBLOQ object, parameters: serial pointer, wifiSsid, WifiPwd, iotId, iotPwd
Obloq olq(&softSerial,"DFRobot-guest","dfrobot@2017","Skv3zKyNb","r1lD3ztJ4b");
static unsigned long currentTime = 0; 
unsigned long messageCount = 1;

void setup()
{
    softSerial.begin(9600);
}

void loop()
{
    olq.update();
    if(millis() - currentTime > 10000)
    {
        currentTime =  millis();
        //向设备发送消息,设备Topic:BJpHJt1VW,消息内容:从1开始累加的计数
        //Publish message to device (Topic:BJpHJt1VW), message contents: accumulated numbers counting from 1.
        olq.publish("BJpHJt1VW", String(messageCount++)); 
    }
}

(addendum 2) Sample code: HTTP access

In the previous sample code, Obloq uses the MQTT protocol to connect to the IoT. In fact, Obloq can also use the HTTP protocol to access the Internet. The following show GET method and POST method based on the HTTP protocol.

Different from the access based on MQTT protocol, HTTP protocol does not need to use iot_id, iot_pwd these two parameters, for example: Obloq olq(&softSerial,"DFRobot-guest","dfrobot@2017");

After successful access, the httpMsgHandle function in the sample code can serially print out the code (system message) and message (specific message) returned by the server.

Note: If Obloq uses HTTP POST to send data, the data sent must be a Json string, and the server receiving the data must also accept Json-type data.

Sample code: HTTP GET (As the version may be updated, if sample code cannot be used, please check latest code repository)

#include "Arduino.h"
#include "SoftwareSerial.h"
#include "Obloq.h"

SoftwareSerial softSerial(10,11);

//生成OBLOQ对象,参数:串口指针,wifiSsid,WifiPwd
//Generate OBLOQ object, parameters: serial pointer, wifiSsid, WifiPwd
Obloq olq(&softSerial,"DFRobot-guest","dfrobot@2017");
static unsigned long currentTime = 0; 

void httpMsgHandle(const String& code,const String& message)
{
    Serial.println("Code: " + code);
    Serial.println("Message: " + message);
}

void setup()
{
    softSerial.begin(9600);
    Serial.begin(115200);
    olq.setHttpMsgHandle(httpMsgHandle);
}
int number = 1;
void loop()
{
    olq.update();
    if(millis() - currentTime > 2000)
    {
        currentTime =  millis();
        olq.get("http://iot_s1.dfrobot.com.cn:3001/apiv2/publish?topic=aaa&iot_name=bbb&iot_pwd=ccc&message="+String(number));
        number++;
    }
}

Sample code: HTTP POST (As the version may be updated, if sample code cannot be used, please check latest code repository)

#include "Arduino.h"
#include "SoftwareSerial.h"
#include "Obloq.h"

SoftwareSerial softSerial(10,11);

//生成OBLOQ对象,参数:串口指针,wifiSsid,WifiPwd
//Generate OBLOQ object, parameters: serial pointer, wifiSsid, WifiPwd
Obloq olq(&softSerial,"DFRobot-guest","dfrobot@2017");
static unsigned long currentTime = 0; 

void httpMsgHandle(const String& code,const String& message)
{
    Serial.println("Code: " + code);
    Serial.println("Message: " + message);
}

void setup()
{
    softSerial.begin(9600);
    Serial.begin(115200);
    olq.setHttpMsgHandle(httpMsgHandle);
}
void loop()
{
    olq.update();
    if(millis() - currentTime > 2000)
    {
        currentTime =  millis();
        // post传递的数据是JSON格式
        // post data is in JSON format
        olq.post("http://192.168.7.123/test","{\"abc\":12222}");
    }
}

results matching ""

    No results matching ""